<?php
namespace app\admin\controller;

use app\common\model\Article as ArticleModel;
use app\common\model\Category as CategoryModel;
use app\common\controller\AdminBase;
use think\Db;

/**
 * 专题管理
 * Class Subject
 * @package app\admin\controller
 */
class Subject extends AdminBase
{

    protected $subject_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->subject_model = Db::name('subject');        
    }

    /**
     * 专题管理
     * @return mixed
     */
    public function index()
    {
        $where = 1;
        if($subject_name = input('get.subject_name'))
            $where .= ' and subject_name like "%'.$subject_name.'%"';
        
        //获取标签列表
        $data = $this->subject_model->where($where)->paginate(10);
        // 获取分页显示
        $page = $data->render();
        // 模板变量赋值
        $this->assign('data', $data->all());
        $this->assign('page', $page);        
        return $this->fetch();
    }

    /**
     * 添加专题
     * @param string $pid
     * @return mixed
     */
    public function add()
    {
        $this->category_model = new CategoryModel();

        $category_level_list = $this->category_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
        return $this->fetch();
    }

    /**
     * 保存专题
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = input('post.');
            unset($data['key']);
            
            $validate_result = $this->validate($data, 'Subject');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['add_time'] = time();
                if ($this->subject_model->insert($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑专题
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $this->category_model = new CategoryModel();

        $category_level_list = $this->category_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
        
        $data = $this->subject_model->find($id);
        //查找文章的名称
        $article_list = Db::name('Article')->field('id,title')->where(['id'=>['in',$data['article_ids']]])->select();

        return $this->fetch('edit', ['data' => $data,'article_list'=>$article_list]);
    }

    /**
     * 更新专题
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = input('post.');
            
            unset($data['key']);
            $validate_result = $this->validate($data, 'Subject');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->subject_model->update($data) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除专题
     * @param $id
     */
    public function delete($id)
    {
        if ($this->subject_model->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    
    /**
     * 获取分类下的所有文章
     */
    public function getArticleById () {
        $id = input('param.id');
        $data = Db::name('Article')->field('id,title')->where(['cid'=>$id])->select();
        $data ? die(handleResult(1, '',$data)) : die(handleResult(0, '该分类下没有文章！'));
    }
    
    /**
     * 获取关键字所有文章
     */
    public function getArticleByKey () {
        $key = input('param.key');
        $where = 1;
        if($key)
            $where .= ' and title like "%'.$key.'%"';
        
        //获取带关键字的文章
        $article_list = Db::name('Article')->field('id,title')->where($where)->select();
        
        $article_list ? die(handleResult(1, '',$article_list)) : die(handleResult(0, '该关键词没有文章！'));
    }
    
    
}